package com.YCH.behavior.service.impl;

import com.YCH.behavior.service.AppCommentReplayBehaviorService;
import com.YCH.model.article.pojos.TbArticle;
import com.YCH.model.article.pojos.TbArticleComment;
import com.YCH.model.article.pojos.TbArticleCommentReplay;
import com.YCH.model.behavior.dtos.userCommentReplayDto;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.*;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.utils.common.ZipUtils;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * @author 16554
 */
@Service
@SuppressWarnings("all")
public class AppCommentReplayBehaviorServiceImpl implements AppCommentReplayBehaviorService {
    @Autowired
    private TbArticleMapper tbArticleMapper;
    @Autowired
    private TbArticleCommentMapper tbArticleCommentMapper;
    @Autowired
    private TbArticleCommentReplayMapper tbArticleCommentReplayMapper;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private TbLikesBehaviorMapper tbLikesBehaviorMapper;
    /**
     * 插入回复
     * @param dto
     * @return
     */
    @Override
    public ResponseResult commentReplay(userCommentReplayDto dto) {
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        if(dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"文章id不能为空！");
        }
        if(dto.getContent() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"评论内容不能为空！");
        }
        // 2.判断该文章是否存在
        TbArticle tbArticle = tbArticleMapper.loadArticleById(dto.getArticleId());
        if(tbArticle == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该文章已被删除或者该文章已近下架！");
        }
        // 3.判断该文章评论是否存在
        TbArticleComment tbArticleComment = tbArticleCommentMapper.selectCommentById(dto.getCommentId());
        if(tbArticleComment == null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"不存在该id的评论！");
        }
        // 4.如果该文章存在，就插入评论
        Integer integer;
        TbArticleCommentReplay tbArticleCommentReplay = new TbArticleCommentReplay();
        tbArticleCommentReplay.setArticleId(dto.getArticleId());
        TbUser tbUser = new TbUser();
        tbUser.setId(userId);
        tbArticleCommentReplay.setUser(tbUser);
        tbArticleCommentReplay.setCommentId(dto.getCommentId());
        if(dto.getCommentReplayId() !=null){
            tbArticleCommentReplay.setCommentReplayId(dto.getCommentReplayId());
        }
        tbArticleCommentReplay.setContent(ZipUtils.gzip(dto.getContent()));
        tbArticleCommentReplay.setCreatTime(new Date());
        if (dto.getCountyId() > 0 ){
            tbArticleCommentReplay.setProvinceId(dto.getProvinceId());
            tbArticleCommentReplay.setCityId(dto.getCityId());
            tbArticleCommentReplay.setCountyId(dto.getCountyId());
            integer = tbArticleCommentReplayMapper.insertHasLocation(tbArticleCommentReplay);
        }else {
            integer = tbArticleCommentReplayMapper.insertNOLocation(tbArticleCommentReplay);
        }
        return ResponseResult.okResult(integer);
    }
}
